<div class="layui-fluid layui-anim febs-anim" id="febs-online" lay-title="在线监控"
     xmlns:shiro="http://www.w3.org/1999/xhtml">
  <div class="layui-row febs-container">
    <div class="layui-col-md12">
      <div class="layui-card">
        <div class="layui-card-body febs-table-full">
          <form class="layui-form layui-table-form" lay-filter="accident-table-form">
            <div class="layui-row">
              <div class="layui-col-md2 layui-col-sm12 layui-col-xs12 table-action-area" style="margin-left: 80%">
                <div class="layui-btn layui-btn-sm layui-btn-primary table-action" id="reset">
                  <i class="layui-icon">&#xe79b;</i>
                </div>
                <div class="layui-btn layui-btn-sm layui-btn-primary table-action action-more"
                     shiro:hasAnyPermissions="session:kickout">
                  <i class="layui-icon">&#xe875;</i>
                </div>
              </div>
            </div>
          </form>
          <table lay-filter="onlineTable" lay-data="{id: 'onlineTable'}"></table>
        </div>
      </div>
    </div>
  </div>
</div>
<script type="text/html" id="online-option-one">

  {{#
  var status = {
  0 : {title: '离线', color: 'volcano'},
  1 : {title: '在线', color: 'green'},
  }[d.status];
  }}
  <span class="layui-badge febs-tag-{{status.color}}">{{ status.title }}</span>
</script>
<script type="text/html" id="online-option-two" xmlns:shiro="http://www.w3.org/1999/xhtml">
  <span shiro:lacksPermission="session:kickout">
        <span class="layui-badge-dot febs-bg-orange"></span>无权限
    </span>
  <a lay-event="del" shiro:hasPermission="session:kickout">
    <i class="layui-icon febs-edit-area febs-red">&#xe640;</i></a>
</script>
<script data-th-inline="none" type="text/javascript">
  layui.use(['dropdown', 'jquery', 'form', 'table', 'febs'], function () {
    var $ = layui.jquery,
        febs = layui.febs,
        form = layui.form,
        table = layui.table,
        dropdown = layui.dropdown,
        $view = $('#febs-online'),
        $query = $view.find('#query'),
        $reset = $view.find('#reset'),
        $searchForm = $view.find('form'),
        tableIns;


    form.render();

    initTable();

    dropdown.render({
      elem: $view.find('.action-more'),
      click: function (name, elem, event) {
        var checkStatus = table.checkStatus('onlineTable');
        if (name === 'kickout') {
          if (!checkStatus.data.length) {
            febs.alert.warn('请选择要踢出的用户');
          } else {
            febs.modal.confirm('踢出用户', '确定强制用户下线？', function () {
              var onlineIds = [];
              layui.each(checkStatus.data, function (key, item) {
                onlineIds.push(item.sessionID);
              });
              deleteonlines(onlineIds.join(','));
            });
          }
        }
      },
      options: [{
        name: 'kickout',
        title: '踢出用户',
        perms: 'session:kickout'
      }]
    });
    table.on('tool(onlineTable)', function (obj) {
      var data = obj.data,
          layEvent = obj.event;
      if (layEvent === 'del') {
        febs.modal.confirm('踢出用户', '确定强制用户下线？', function () {
          deleteonlines(data.sessionID);
          console.log(data);
        });
      }
    });

    $query.on('click', function () {
      tableIns.reload({page: {curr: 1}});
    });

    $reset.on('click', function () {
      $searchForm[0].reset();
      tableIns.reload({page: {curr: 1}});
    });

    function initTable() {
      tableIns = febs.table.init({
        elem: $view.find('table'),
        id: 'onlineTable',
        url: ctx + 'session/list',
        cols: [[
          {type: 'checkbox'},
          {title: '用户名称', field: 'username', minWidth: 100},
          {title: '登录时间', field: 'startTimestamp'},
          {title: '最后访问时间', field: 'lastAccessTime'},
          {title: 'IP地址', field: 'systemHost'},
          {title: '登录地点', field: 'location'},
          {title: '状态', templet: '#online-option-one'},
          {title: '操作', toolbar: '#online-option-two', minWidth: 140}
        ]]
      });
    }

    function deleteonlines(onlineIds) {
      febs.post(ctx + 'session/kickout/' + onlineIds, null, function () {
        febs.alert.success('已将该用户强制下线');
        $query.click();
      });
    }
  })
</script>